<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Back Date</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="./EldercareMasterPage/_finalstyle.css" rel="stylesheet" />
	<link rel="stylesheet" href="./jquery/style.css" type="text/css" media="screen"/>
	<link rel="stylesheet" href="./jquery/jquery-ui.css" />
	<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="./css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
</head>
<?php
	define('CONFIG_FILE',true);
	include './config.php';
	define('DB_FILE',true);

	session_start();
	if (empty($_SESSION["username"])||empty($_SESSION["userType"])) {
		Header("Location: ./Index.php");
		exit();
	}else{
		$userType = $_SESSION["userType"];
	}
	if($userType=="Therapy Assistant"){
		$url="./Index.php";
		include_once './Tool/Common/Redirect.php';
		exit();
	}	
	// set timeout period in seconds
	$inactive = 7200;
	// check to see if $_SESSION['timeout'] is set
	if (isset($_SESSION["timeout"])) {
		$session_life = time() - $_SESSION["timeout"];
		if ($session_life > $inactive) {
			session_destroy();
			Header("Location: Timeout.php");
		}
	}
	$_SESSION["timeout"] = time();
?>
<body onLoad="document.formnric.barcode.focus();">                                                                                                                         <ul>
	<div class="banner"></div>
		<div id="main">
			<div class="left-wrapper">
				<div class="logo">
					<img src="images/eldercare.gif" alt="logo" width="240px" />
				</div>
				<div class="menu-wrapper">
					<div id="cssmenu">        
							<?php
							if ($userType == "Therapy Assistant") {
								include_once('./Sidebar/_SidebarTA.php');
							} else if ($userType == "Centre Manager") {
								include_once('./Sidebar/_SidebarCM.php');
							} else {
								include_once('./Sidebar/_SidebarSA.php');
							}
							?>                     
					</div>                    
				</div>
				<div class="clear"></div>
			</div>
			<div id="content">
				<div class="content2">
					<b style="float:right; margin: 0px 0 0 0">  
						<?php
							if (!empty($_SESSION["centreName"])) {
								$Username = $_SESSION["username"];
								$centreName = $_SESSION["centreName"];
								echo "Welcome, " . $Username . " ! " . $centreName . " Branch";
							} else {
								$Username = $_SESSION["username"];
								echo "Welcome, " . $Username;
							}
						?>
						<input type="submit" class="btn btn-danger" value="Logout" onclick="parent.location = 'logout.php'">
					</b>
                        </p>
						<br>
						<div style="float:center">
						<br>
						<h3>BackDate</h3>
						<br>
                        <td rowspan="3" style="vertical-align:middle;text-align:center;width:70%"></td>
                        <p> 
							<b>Please select a date:</b> 
							<form method = "post" class="form-horizontal"> 
								<div class="control-group">
									<div class="controls input-append date form_date" data-link-field="dtp_input1">
										<input  size="16" type="text" value="" readonly>
										<span class="add-on"><i class="icon-remove"></i></span>
										<span class="add-on"><i class="icon-th"></i></span>
									</div>
									<input name="datepicker"  type="hidden" id="dtp_input1" value="" />
									<input type="submit" button class="btn"id="submit" value ='Submit' name="BackDate"/>
								</div>	
							</form>
						</p>
                        <table  cellpadding="0" cellspacing="0" style="width: 100%; height: 100px;">
                            <tr>
                                <td style="width: 20%; height: 20%;">
                                    <?php
										echo"
										<p style='vertical-align:middle;text-align:center'><b>Scan NRIC: </b>
										<form  method='POST' name='formnric'>&nbsp;&nbsp;
										<input id='barcode'class='tb10'type='text' name='barcode' size='10' maxLength='9'/>
										<input id = 'selectedDate' class='tb10' type = 'hidden' name = 'selectedDate' value = '";
										echo isset($_POST['BackDate']) ? $_POST['datepicker'] : '';
										echo" /></form></p>";

										if (isset($_POST['barcode'])) {
											
										} else {
											echo "<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'>Please enter the NRIC!</font></p>";
										}                                 				
                                    ?>
                                </td>
                                <td rowspan="3" valign="top"align="center"style="width:100%"><p></p>
                                    <?php
                                    	require_once './Tool/Common/PrepareInput.php';

		                                if (isset($_POST['BackDate'])){
		                                	if(!empty($_POST['datepicker'])){
		                            			$date = date('Y-m-d',strtotime(prepareInput($_POST['datepicker'])));
		                            			if(!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)){
		                            				echo "<font color='red'>Invalid Date Format";
		                            			}else{
			                                   		if ($date < date('Y-m-d')) {
				                                        if (empty($centreName)) {
				                                        	$_SESSION['selectedBackDate'] = $date;
				                                            include_once("BackDate/sugetbackdate.php");
				                                        } else {
				                                        	$_SESSION['selectedBackDate'] = $date;
				                                            include_once("BackDate/getbackdate.php");
				                                        }		                               
				                                    } else {
				                                        echo "<font color='red'> You can only BackDate the day before today!";
				                                    }
				                                }
			                                }else {
		                                   		echo " <font color='red'> Please select a date! </font>";
		                                   	}
		                                }
		                                if (isset($_POST['barcode'])) {		                                
											if (preg_match("/^[0-9A-Za-z]*$/",prepareInput($_POST['barcode'])) && strlen(prepareInput($_POST['barcode']))== 9) {
												if (empty($centreName)) {
													include_once("BackDate/sumakebackdate.php");
													$_POST['BackDate'] = $_POST['datepicker'] = $_POST['selectedDate'];
													$date = date('Y-m-d',strtotime(prepareInput($_POST['BackDate'])));
													if(!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)){
		                            					echo "<font color='red'>Invalid Date Format";
		                            					exit();
		                            				}
													echo "<p></p>";
													include_once("BackDate/sugetbackdate.php");
												} else {
													echo "<p></p>";
													include_once("BackDate/makebackdate.php");
													$_POST['BackDate'] = $_POST['datepicker'] = $_POST['selectedDate'];
													$date = date('Y-m-d',strtotime(prepareInput($_POST['BackDate'])));
													if(!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)){
		                            					echo "<font color='red'>Invalid Date Format";
		                            					exit();
		                            				}
													include_once("BackDate/getbackdate.php");
												}
											} else {
												echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'>Invalid NRIC!</font>";
											}			                                
		                                }
                                    ?>
                                </td>
                            <tr>
                                <td style="vertical-align:middle;text-align:center; width:20%">
                                </td></tr>		<tr></tr>
                            <tr>
                                <td style="width:20%">
                            </tr>
					</table>
				</div>
				<div class="clear"></div>
			</div>
		</div>
	<div id="footer">&copy; 2013 MP22 IIT-IT Temasek Polytechnic</div>
</body> 
<script type="text/javascript" src="./js/jquery-1.8.3.min.js" charset="UTF-8"></script>
<script type="text/javascript" src="./js/bootstrap.min.js"></script>
<script type="text/javascript" src="./js/bootstrap-datetimepicker.js" charset="UTF-8"></script>
<script type="text/javascript">
	$('.form_date').datetimepicker({
		format:"dd/mm/yyyy",
        weekStart: 0,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0,
		pickerPosition:'bottom-left',
		daysOfWeekDisabled: "0,6",
    });
</script>
</html>